<?php
/*
	商品独立佣金设置
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'单品规则列表'),
	array('op'=>'edit','title'=>'添加/编辑单品规则'),
);
set_html('tabs',$tabs);
$setting=$this->setting;
if($_RQ['op']=='list'){
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['goods_id']){
		$params['goods_id']=intval($_RQ['goods_id']);
		$return['params']['goods_id']=$_RQ['goods_id'];
	}
	$psize=15;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	$list=$this->get_list('goods',$parameter,$total);
	if($list){
		$params=array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1);
		foreach($list as &$item){
			$item['goods']=pdo_get('jy_weishop_goods',array('id'=>$item['goods_id']),array('title','image'));
			$params['goodsid']=$item['goods_id'];
			$item['total_price']=pdo_getsum('jy_weishop_order_goods',$params,'total_price');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：<red>店铺/部门/个人销售额=销售额-单品销售额；</red>
		</br>使用场景：所有员工，都是后台指定开通的；
		</br>1、直推佣金：阶梯式的，每月个人总销售额N万，直推佣金%！
		</br>2、部门分红：阶梯式的，每月部门总销售额N万，直推佣金%
		</br>3、店铺分红：阶梯式的，每月整店总销售额N万，直推佣金%
		','list'=>array(
			array('name'=>'goods.title','title'=>'商品名称','type'=>'string'),
			array('name'=>'goods.image','title'=>'商品图片','type'=>'image'),
			array('name'=>'total_price','title'=>'单品销售额','type'=>'string'),
			array('name'=>'ignore','title'=>'是否关闭分红','type'=>'switch','set'=>'set'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>18,'action'=>'jy_merch/manage.get_goods/','name'=>'goods_id','title'=>'选择商品'),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$data=array(
			'goods_id'=>intval($post['goods_id']),
			'ignore'=>intval($post['ignore']),
			'tui_per'=>ijson_encode($post['tui_per']),
			'bm_per'=>ijson_encode($post['bm_per']),
			'dian_per'=>ijson_encode($post['dian_per']),
		);
		$id=intval($post['id']);
		if($id){
			$this->update('goods',$data,array('id'=>$id));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['merchid']=$_SESSION['onemerchid'];
			$this->insert('goods',$data);
		}
		exi('设置成功！','','list');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=$this->get('goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['tui_per']=$item['tui_per']?json_decode($item['tui_per'],true):array();
		$item['bm_per']=$item['bm_per']?json_decode($item['bm_per'],true):array();
		$item['dian_per']=$item['dian_per']?json_decode($item['dian_per'],true):array();
	}else{
		$item=array(
			'title'=>'',
			'commissions'=>array(),
		);
	}
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'单品规则设置',
		'edit'=>array(
			array('name'=>'goods_id','title'=>'选择商品','type'=>'choose','action'=>'jy_merch/manage.get_goods/'),
			array('name'=>'ignore','title'=>'是否关闭分红','type'=>'switch','help'=>'关闭后，该商品将不参与分销'),
			array('type'=>'array','title'=>'直推佣金','name'=>'tui_per','add_tip'=>'添加一个阶梯',
					'list'=>array(
						array('type'=>'unitstring','unit'=>'元','name'=>'man','title'=>'满N元'),
						array('type'=>'unitstring','unit'=>'%','name'=>'rate','title'=>'分红%'),
					),
					'details'=>array(
						array('type'=>'unitstring','unit'=>'元','name'=>'man','title'=>'满N元'),
						array('type'=>'unitstring','unit'=>'%','name'=>'rate','title'=>'分红%'),
			)),
			array('type'=>'array','title'=>'部门分红','name'=>'bm_per','add_tip'=>'添加一个阶梯',
					'list'=>array(
						array('type'=>'unitstring','unit'=>'元','name'=>'man','title'=>'满N元'),
						array('type'=>'unitstring','unit'=>'%','name'=>'rate','title'=>'分红%'),
					),
					'details'=>array(
						array('type'=>'unitstring','unit'=>'元','name'=>'man','title'=>'满N元'),
						array('type'=>'unitstring','unit'=>'%','name'=>'rate','title'=>'分红%'),
			)),
			array('type'=>'array','title'=>'店铺分红','name'=>'dian_per','add_tip'=>'添加一个阶梯',
					'list'=>array(
						array('type'=>'unitstring','unit'=>'元','name'=>'man','title'=>'满N元'),
						array('type'=>'unitstring','unit'=>'%','name'=>'rate','title'=>'分红%'),
					),
					'details'=>array(
						array('type'=>'unitstring','unit'=>'元','name'=>'man','title'=>'满N元'),
						array('type'=>'unitstring','unit'=>'%','name'=>'rate','title'=>'分红%'),
			)),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['ignore'])){
		$update['ignore']=intval($post['ignore']);
	}
	if($update){
		$this->update('goods',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			$this->delete('goods',array('id'=>$id));
		}
	}
	exi('删除成功！');
}